<template>
    <div id="login-container">
        <!--  登录框的head -->
        <div style="text-align: center; height: 50px">登录</div>

        <!--  登录的表单框,需要导入组件element-ui  -->
        <el-form v-bind:model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <!-- 账号行=标签+输入框 -->
            <el-form-item label="账号" prop="user">
                <el-input type="text" v-model="ruleForm.user" autocomplete="off"></el-input>
            </el-form-item>

            <!--  密码行=标签+密码框 -->
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>

            <!--  提交重置行=提交按钮+重置按钮 -->
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            //校验用户名
            var validateUser = (rule,value,callback) => {
                if(value===''){
                    callback(new Error("请输入账户"));
                }else{
                    if(this.ruleForm.pass !==''){
                        this.$refs.ruleForm.validateField('pass');
                    }
                }
                callback();
            };

            //校验用户名
            var validatePass = (rule, value, callback) => {
                if(value ===''){
                    callback(new Error('请输入密码'))
                }else{
                    callback();
                }
            };

            return {
                //数据
                ruleForm: {
                    user:'',
                    pass:''
                },
                //校验规则
                rules:{
                    user:[{validator: validateUser, trigger:'blur'}],
                    pass:[{validator: validatePass, trigger:'blur'}],
                },
                isLogin:false
            }
        },
        methods:{
            //提交接口
            submitForm(formName){
                //尝试全局get和set状态
                this.$store.commit('increment');
                console.log(this.$store.state.count);

                this.$message.success(formName + "表单登录成功！");
                this.$router.push({path:"/"});
            },
            //重置接口
            resetForm(formName){
                this.$refs[formName].resetFields()
            }
        }
    }
</script>

<style scoped>
    body{
        margin: 0;
    }
    #login-container{
        width: 400px;
        height: 290px;
        background: #e5e9f2;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -220px;
        margin-top: -170px;
        border-radius: 5px;
        padding-top: 40px;
        padding-right: 40px;
    }
</style>
